﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.IRepository.MiniSite;
using YunSpace.Repository.MiniSite;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Domain.ViewModel.MiniSite;
using MySql.Data.MySqlClient;
using System.Data.Common;
using YunSpace.Data;
#endregion

namespace YunSpace.Application.MiniSite
{
    public class FavoriteApp
    {
        private IFavoriteRepository service = new FavoriteRepository();

        public List<FavoriteEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<FavoriteEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public FavoriteEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(FavoriteEntity favoriteEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                favoriteEntity.Modify(keyValue);
                service.Update(favoriteEntity);
            }
            else
            {
                favoriteEntity.Create();
                // favoriteEntity.F_Category = 2;
                service.Insert(favoriteEntity);
            }
        }
        public FavoriteEntity GetFormEntity(string F_Type, string F_CustomerId ,string F_FavoriteId)
        {
            var result = service.FindEntity(p=>p.F_CustomerId==F_CustomerId && p.F_FavoriteId==F_FavoriteId && p.F_Type== F_Type );
            return result;
        }
        /// <summary>
        /// 获取我的收藏-场地list
        /// </summary>
        /// <param name="lastId">最后一个id</param>
        /// <param name="count">获取条数</param>
        /// <param name="F_CustomerId">用户ID</param>
        /// <returns></returns>
        public List<FavoriteSitesActionModel> GetFavoriteSites(string lastId, int count, string F_CustomerId)
        {
            // 最新创建的 10 条数据 小场地类型为 F_Category = 1

            using (var db = new RepositoryBase())
            {
                //场地收藏
                var result = new List<FavoriteSitesActionModel>();

                var strSql = @"select f.F_Id, f.F_FavoriteId,s.title ,sp.units,min(sp.inner_price)inner_price,p.path from MiniSite_Favorite f 
                            left join SL_Site s on f.F_FavoriteId= s.F_id  and f.F_Status =1 and f.F_Type=1  and f.F_CustomerId=@F_CustomerId   inner join SL_Space sp on s.F_Id = sp.F_SiteId LEFT join SL_Photos p  
                            on s.F_Id = p.photoable_id and p.is_cover=1
                            {0}  group by f.F_FavoriteId  order by p.is_cover,f.F_LastModifyTime desc  LIMIT 0, {1};";

                if (lastId.IsNullOrEmpty())
                {
                    strSql = string.Format(strSql, "", count);
                }
                else
                {
                    strSql = string.Format(strSql, "WHERE  f.F_Id  < @fId", count);
                }
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_CustomerId", F_CustomerId)
                };
                result = db.FindList<FavoriteSitesActionModel>(strSql, parameter);
                return result;
            }

        }

        /// <summary>
        /// 获取我的收藏-活动list
        /// </summary>
        /// <param name="lastId">最后一个id</param>
        /// <param name="count">获取条数</param>
        /// <param name="F_CustomerId">用户ID</param>
        /// <returns></returns>
        public List<FavoriteActivityActionModel> GetFavoriteActivity(string lastId, int count, string F_CustomerId)
        {
            // 最新创建的 10 条数据 小场地类型为 F_Category = 1

            using (var db = new RepositoryBase())
            {//活动收藏
                var result1 = new List<FavoriteActivityActionModel>();

                var strSql = @"select f.F_Id,s.F_Id as s_id,s.F_Subject,f.F_FavoriteId,s.F_ConsumeType,p.path ,p.name,p.tag 
                                from MiniSite_Favorite f  INNER  JOIN   MiniSite_SiteActivity s on f.F_FavoriteId=s.F_Id  and f.F_Status =1 and f.F_Type=2 and f.F_CustomerId= @F_CustomerId 
                                 LEFT  join SL_Photos p  on p.photoable_id= s.F_Id and p.is_cover=1
                                  {0}  GROUP BY f.F_FavoriteId   ORDER  BY  f.F_CreatorTime  DESC   LIMIT 0, {1};";

                if (lastId.IsNullOrEmpty())
                {
                    strSql = string.Format(strSql, "", count);
                }
                else
                {
                    strSql = string.Format(strSql, "WHERE  f.F_Id <  @fId", count);
                }
                DbParameter[] parameter =
                {
                        new MySqlParameter("@fId", lastId),
                        new MySqlParameter("@F_CustomerId", F_CustomerId)
                    };
                result1 = db.FindList<FavoriteActivityActionModel>(strSql, parameter);
                return result1;
            }
        }

        /// <summary>
        /// 获取我的攻略-攻略list
        /// </summary>
        /// <param name="lastId">最后一个id</param>
        /// <param name="count">获取条数</param>
        /// <param name="F_CustomerId">用户ID</param>
        /// <returns></returns>
        public List<FavoriteStrategyActionModel> GetStrategys(string lastId, int count, string F_CustomerId)
        {

            using (var db = new RepositoryBase())
            {//攻略收藏
                var result1 = new List<FavoriteStrategyActionModel>();

                var strSql = @"select f.F_Id,f.F_FavoriteId, s.F_outline,s.F_title, p.path  photoUrl
                                from MiniSite_Favorite f  INNER  JOIN   MiniSite_Strategy s on f.F_FavoriteId=s.F_Id  and f.F_Status =1 and f.F_Type=3 and f.F_CustomerId= @F_CustomerId 
                                 LEFT  join SL_Photos p  on p.photoable_id= s.F_Id and p.is_cover=1
                                  {0}  GROUP BY f.F_FavoriteId   ORDER  BY  f.F_CreatorTime  DESC   LIMIT 0, {1};";

                if (lastId.IsNullOrEmpty())
                {
                    strSql = string.Format(strSql, "", count);
                }
                else
                {
                    strSql = string.Format(strSql, "WHERE  f.F_Id > @fId", count);
                }
                DbParameter[] parameter =
                {
                        new MySqlParameter("@fId", lastId),
                        new MySqlParameter("@F_CustomerId", F_CustomerId)
                    };
                result1 = db.FindList<FavoriteStrategyActionModel>(strSql, parameter);
                return result1;
            }
        }
    }
}

